package com.chuangyang.fixboxmaster.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.chuangyang.fixboxlib.widgets.SlidingTabLayout;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.ui.fragment.CashRepairRequestFragment;
import com.chuangyang.fixboxmaster.ui.fragment.OrderListFragment;

import java.util.ArrayList;

/**
 * Created by vanshu on 15/8/6 13:39.
 */
public class BalanceDetailActivity extends BaseActivity {

    private String[] mTitle = new String[]{"维修记录", "提现记录"};
    private ArrayList<CashRepairRequestFragment> mFragments = new ArrayList<>();
    private int mSelectPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("账户明细");
        setContentView(R.layout.activity_balance_detail);
        ViewPager mViewPager = (ViewPager) findViewById(R.id.viewPager);
        ViewPagerAdapter mAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(mTitle.length);
        SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setCustomTabView(R.layout.home_tab_indicator, android.R.id.text1);
        mSlidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.tab_selected_strip));
        mSlidingTabLayout.setDistributeEvenly(true);
        mSlidingTabLayout.setViewPager(mViewPager);
        mSlidingTabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mSelectPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                enableDisableSwipeRefresh(state == ViewPager.SCROLL_STATE_IDLE);
            }
        });
    }


    public class ViewPagerAdapter extends FragmentStatePagerAdapter {
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Bundle bundle;
            switch (i) {
                case 0:
                    bundle = new Bundle();
                    bundle.putInt(CashRepairRequestFragment.REQUEST_TYPE, CashRepairRequestFragment.CASH_INCOME_HISTORY);
                    CashRepairRequestFragment mIncomeRequestFragment = new CashRepairRequestFragment();
                    mIncomeRequestFragment.setArguments(bundle);
                    mFragments.add(mIncomeRequestFragment);
                    return mIncomeRequestFragment;
                case 1:
                    bundle = new Bundle();
                    bundle.putInt(CashRepairRequestFragment.REQUEST_TYPE, CashRepairRequestFragment.CASH_REQUEST_HISTORY);
                    CashRepairRequestFragment mCashRequestFragment = new CashRepairRequestFragment();
                    mCashRequestFragment.setArguments(bundle);
                    mFragments.add(mCashRequestFragment);
                    return mCashRequestFragment;
            }
            return null;
        }

        @Override
        public int getCount() {
            return mTitle.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle[position];
        }
    }


    protected void requestDataRefresh() {
        mFragments.get(mSelectPosition).updateListView(new OrderListFragment.OnPullRefreshListener() {
            @Override
            public void onPullRefresh() {
                onRefreshingStateChanged(false, 500);
            }
        });
    }



}
